function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }

    return null;
} 

function processSimpleDoc(doc) {
    if (!doc) {
        $("#loginMessage").css("opacity", "1");
        return;
    }
    $("#loginOverlay").css("display", "none");
    $("#container").css("display", "block");
    
    var loaded = document.createElement("div");  // Create with DOM
    loaded.innerHTML = doc;
    $("div#title h1").text($(loaded).find("div#title h1").text());
    document.title = $(loaded).find("div#title h1").text();
    $("div#title p").text($(loaded).find("div#title p").text());
    $("div#title").css("opacity", "1");
    $("div#main").html($(loaded).find("div#main").html());
    $("div#secondary").html($(loaded).find("div#secondary").html());
    $("div.slide").each(function(index, slide) {
        Google.fetchVideoURL($(slide), videoURLCallback);
    });

    Player = new window.Player($("#player"), $("#modeselector_container"), $(loaded).find("div#title").attr("data-video-uid"));
    container = $("#container");
    ModeSelector = new window.ModeSelector(container, Player);
    ActiveSlideController = new window.ActiveSlideController(container, Player);
    Player.play();    
    //$("#main").empty().append(doc);
}

function videoURLCallback(slide, url) {
    $(slide).attr("data-video", url);
}

function googleAuthCallback(Google, isLoggedIn, errorMessage) {
    if (isLoggedIn) {
        var fileId = window.getUrlParameter("docId");
        Google.fetchSimpleDocument(fileId, processSimpleDoc);
    }
    else {
        $("#loginOverlay").css("display", "block");
        $("#loginMessage").css("opacity", "1");
        $("#container").css("display", "none");
    }
}

$(window).resize(function() {
    if (Player.aspectRatio) {
        Player.resizeVideo();
    }
});

$(document).ready(function () {

    try {

        var videoURLs = {};
        if (null == window.getUrlParameter("docId")) {
            // No document id specified
            throw "404 - The requested document cannot be found";
        }

        Google = new window.Google( "865748652115-vaemdrcrnfp3ukhunv3qtmitend3fh9r.apps.googleusercontent.com", 
                                        "AIzaSyBQaANhaTRMskd75UiglC1lYtyWdhhtE-I",
                                        googleAuthCallback);


        $("#login_btn").click(function () {
            Google.auth(googleAuthCallback);
        });

        $("#startbutton_play").click(function(){
            $("body, html").animate({
                scrollTop: $(".slide").first().offset().top
            }, 500);
            $(".slide").first().click();
            $("#ms_leanback").click();        
        });
        $("#startbutton_skim").click(function() {
            $("body, html").animate({
                scrollTop: $(".slide").first().offset().top
            }, 500);
            ga('send', 'pageview', '/browse/' + googleProfile.displayName.split(' ').pop() + "/" +
                window.getUrlParameter("docId") );
            $("#ms_skimmer").click();
        });
        $("#startbutton_read").click(function(){
            $("body, html").animate({
                scrollTop: $(".slide").first().offset().top
            }, 500);
            ga('send', 'pageview', '/read/' + googleProfile.displayName.split(' ').pop() + "/" +
                window.getUrlParameter("docId") );
            $("#ms_reader").click();
        });
        $(document).scroll(function() {
            var a = $(".active_slide").offset();
            if (!a) return;
            var offset = a.top - $("body").scrollTop();
            $("#toactive_container").toggleClass("shown", (Math.abs(offset) > $(window).height() * .5));
        });

    } catch(err) {
        $("body").empty().append("<h1>"+err+"</h1>");
    }

});

